/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jifTerminarInventario.java
 *
 * Created on 22-sep-2010, 14:55:15
 */

package Inventario;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_Fecha;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.VerificarDatos;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.MaskFormatter;
import suministros.ejb30.entity.Inventario;
import suministros.ejb30.entity.Parametros;

/**
 *
 * @author Diana
 */
public class jifTerminarInventario extends javax.swing.JInternalFrame {
     private MaskFormatter mascaraFechaIngreso;
    /** Creates new form jifTerminarInventario */
    public jifTerminarInventario() {
        if(Listas.getInventario()){
             construirMascaras();
             initComponents();

        }else{
           Utiles_MensajesDialogo.mensajeAlerta("No se ha iniciado un inventario actualmente");
           this.dispose();
        }

    }
       private void construirMascaras() {
    try {
      this.mascaraFechaIngreso = new MaskFormatter("##/##/####");
    } catch (ParseException ex) {
      Logger.getLogger(jifIniciarInventario.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
  }
      public void terminarInventario(){
        try {
            Parametros param = Listas.getParametrosID("SU1");
            String ano = param.getParam1();
            String mes = param.getParam2();
            Inventario inv = new Inventario();
            Date fecha = new Date(VerificarDatos.verificaFecha(this.jtfFecha.getText()).getTime());
            Utiles_Fecha utf = new Utiles_Fecha(fecha);
            inv.setCodInventario(Listas.retornaInventario().getCodigo());
            inv.setFechaFin(fecha);
            inv.setEstado(false);
            Boolean error=GestionPedidosDelegate.getInstance().terminarInventario(inv);
            Utiles_MensajesDialogo.mensajeExito();
            this.jbtGuardar.setEnabled(false);
            Listas.setInventario();
        } catch (Exception ex) {
             Utiles_MensajesDialogo.mensajeError("No se ha podido terminar el inventario");
            Logger.getLogger(jifIniciarInventario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfFecha = new javax.swing.JFormattedTextField(this.mascaraFechaIngreso);
        jbtGuardar = new javax.swing.JButton();
        jbSalir = new javax.swing.JButton();

        setIconifiable(true);
        setTitle("TERMINAR PROCESO DE INVENTARIO");

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("FECHA DE TERMINO DE INVENTARIO:");

        jtfFecha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfFechaKeyPressed(evt);
            }
        });

        jbtGuardar.setFont(new java.awt.Font("Arial", 1, 12));
        jbtGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbtGuardar.setMnemonic('A');
        jbtGuardar.setText("ACEPTAR");
        jbtGuardar.setEnabled(false);
        jbtGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtGuardarActionPerformed(evt);
            }
        });

        jbSalir.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jbSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbSalir.setMnemonic('C');
        jbSalir.setText("CERRAR");
        jbSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jbtGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbSalir)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtGuardar)
                    .addComponent(jbSalir))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfFechaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfFechaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbtGuardar.setEnabled(true);
            this.jbtGuardar.requestFocus();

        }
}//GEN-LAST:event_jtfFechaKeyPressed

    private void jbtGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtGuardarActionPerformed
        int res = javax.swing.JOptionPane.showConfirmDialog( this,"¿Esta seguro que desea terminar el inventario? \n No se podran realizar ajustes de inventario",
                "Confirmar",javax.swing.JOptionPane.YES_NO_OPTION );
        if(res==javax.swing.JOptionPane.YES_OPTION){
            if(this.jtfFecha.getText().compareTo("")!=0){
                try {
                    this.terminarInventario();
                } catch (Exception ex) {
                    Logger.getLogger(jifIniciarInventario.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else{
                Utiles_MensajesDialogo.mensajeError("No se puedo iniciar el inventario");
                return;
            }


        }
}//GEN-LAST:event_jbtGuardarActionPerformed

    private void jbSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalirActionPerformed
        int res = javax.swing.JOptionPane.showConfirmDialog( this,"Esta seguro de salir?",
                "Confirmar",javax.swing.JOptionPane.YES_NO_OPTION );
        if(res==javax.swing.JOptionPane.YES_OPTION){
            this.dispose();
        }
}//GEN-LAST:event_jbSalirActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbSalir;
    public javax.swing.JButton jbtGuardar;
    private javax.swing.JFormattedTextField jtfFecha;
    // End of variables declaration//GEN-END:variables

}
